<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreCompletionRequest;
use App\Models\Completion;
use App\Models\CompletionType;
use App\Services\CompletionService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CompletionController extends Controller
{
    protected $CompletionService;
    public function __construct(CompletionService $CompletionService)
    {
        $this->CompletionService = $CompletionService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $type_id = $Request->type_id ?? 0;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        
        $sql = $this->CompletionService->get($search);
        if(Auth::user()->company_id > 0){
            $sql->where('project_id',$Request->project_id);
        }
        $sql->when($type_id, function ($query, $type_id) {
            return $query->where(function($query) use ($type_id) {
                $query->where('type_id',$type_id);
            });
        });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        $type = CompletionType::where('project_id',$Request->project_id)->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data,'type'=>$type]);
    }


    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Completion::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新数据。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $Request)
    {
        $POSTONLY = Completion::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->CompletionService->insert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = Completion::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->CompletionService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Completion::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function types(Request $Request) {
        $data = CompletionType::where('project_id',$Request->project_id)->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新类型。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function storeType(Request $Request)
    {
        $POSTONLY = CompletionType::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->CompletionService->insertType($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function updateType(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = CompletionType::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->CompletionService->updateType($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function deleteType(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = CompletionType::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
